package com.changgou.order.controller;


import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.OrderService;
import com.changgou.order.pojo.Order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private TokenDecode tokenDecode;

    @PostMapping("add")
    public Result<String> add(@RequestBody Order order) {
        String username = tokenDecode.getUserInfo().get("username");
        order.setUsername(username);
        String orderId = orderService.add(order);
        return new Result(true, StatusCode.OK, "添加成功", orderId);
    }

    /**
     * @param id
     * @return com.changgou.entity.Result
     * @description: 根据订单id查询订单信息
     * @author 大佬味的小男孩
     * @date 2020/08/04 19:20
     */
    @GetMapping("/{id}")
    public Result<Order> findById(@PathVariable("id") String id) {
        Order order = orderService.findById(id);
        return new Result<Order>(true, StatusCode.OK, "查询成功", order);
    }


    /**
     * @param orders
     * @return com.changgou.entity.Result
     * @description: 批量发货
     * @author 大佬味的小男孩
     * @date 2020/08/07 09:47
     */
    @PostMapping("/batchSend")
    public Result batchSend(@RequestBody List<Order> orders) {
        orderService.batchSend(orders);
        return new Result(true, StatusCode.OK, "发货成功");
    }

    /**
     * @param
     * @return
     * @description: 手动收货
     * @author 大佬味的小男孩
     * @date 2020/08/07 10:24
     */
    @PutMapping("/take/{orderId}/operator/{operator}")
    public Result take(@PathVariable String orderId, @PathVariable String operator) {
        orderService.confirmTask(orderId, operator);
        return new Result(true, StatusCode.OK, "");
    }
}
